<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../../ext/ext-all-debug.js"></script>
    <script type="text/javascript" src="../../../../../lib/GeoExt.js"></script>

    <script type="text/javascript">
    
        function test_init(t) {
            t.plan(11);

            /*
             * Set up
             */
            var map, layer, layers, selectControl, store, sm;
            
            map = new OpenLayers.Map();
            layer = new OpenLayers.Layer.Vector("vector");
            layers = [layer];
            map.addLayers(layers);

            store = new GeoExt.data.FeatureStore({layer: layer});

            /*
             * Test
             */

            // create a feature selection model
            // 1 test
            sm = new GeoExt.grid.FeatureSelectionModel();
            t.ok(sm instanceof Ext.grid.RowSelectionModel,
                 "a feature selection model is a row selection model");

            // create a feature selection model and give it a
            // select feature control
            // 2 tests
            selectControl = new OpenLayers.Control.SelectFeature(layer);
            sm = new GeoExt.grid.FeatureSelectionModel({
                selectControl: selectControl, multiple: false
            });
            t.ok(sm.selectControl == selectControl,
                 "ctor sets the passed select feature control in the instance");
            t.eq(sm.singleSelect, true,
                 "ctor sets singleSelect to true in the instance");

            // create a feature selection model with singleSelect true and give
            // it a select feature control
            // 1 test
            selectControl = new OpenLayers.Control.SelectFeature(layer);
            sm = new GeoExt.grid.FeatureSelectionModel({
                singleSelect: true,
                selectControl: selectControl
            });
            t.eq(sm.selectControl.multiple, false,
                 "ctor configures the select feature control with multiple false");

            // create a feature selection model and give it a layer and
            // a select control config
            // 3 tests
            sm = new GeoExt.grid.FeatureSelectionModel({
                layer: layer,
                selectControl: {
                    hover: true
                }
            });
            t.ok(sm.selectControl instanceof OpenLayers.Control.SelectFeature,
                 "ctor creates a select feature control when passed a layer");
            t.ok(sm.selectControl.layer == layer,
                 "ctor configures the select feature control with the passed layer");
            t.eq(sm.selectControl.hover, true,
                 "ctor configures the select feature control with the passed config");

            // create a feature selection model and create a grid with it
            // 3 tests
            sm = new GeoExt.grid.FeatureSelectionModel({
                selectControl: {
                    hover: true
                }
            });
            var grid = new Ext.grid.GridPanel({
                renderTo: "grid",
                store: store,
                columns: [{
                    header: "name"
                }],
                sm: sm,
                deferRowRender: false
            });
            t.ok(sm.selectControl instanceof OpenLayers.Control.SelectFeature,
                 "init creates a select feature control ");
            t.ok(sm.selectControl.layer == layer,
                 "init configures the select feature control with the store layer");
            t.eq(sm.selectControl.hover, true,
                 "init configures the select feature control with the passed config");
            grid.destroy();

            // 1 test
            var CheckboxSelectionModel = Ext.extend(
                Ext.grid.CheckboxSelectionModel,
                GeoExt.grid.FeatureSelectionModelMixin
            );
            sm = new CheckboxSelectionModel();
            t.ok(sm instanceof Ext.grid.CheckboxSelectionModel,
                 "instance is a checkbox selection model");
        }

        function test_row_selection(t) {
            t.plan(7);

            /*
             * Set up
             */

            var map, layer, features, store, sm, grid, e;

            map = new OpenLayers.Map('map');

            layer = new OpenLayers.Layer.Vector("vector");
            map.addLayer(layer);

            features = [
                new OpenLayers.Feature.Vector(null,
                    {foo: "foo1", bar: "bar1"}
                ),
                new OpenLayers.Feature.Vector(null,
                    {foo: "foo2", bar: "bar2"}
                )
            ];
            
            store = new GeoExt.data.FeatureStore({
                layer: layer,
                data: features
            });

            sm = new GeoExt.grid.FeatureSelectionModel();

            grid = new Ext.grid.GridPanel({
                renderTo: "grid",
                store: store,
                columns: [{
                    dataIndex: "foo"
                }, {
                    dataIndex: "bar"
                }],
                sm: sm,
                deferRowRender: false
            });

            /*
             * Test
             */

            // simulate a mousedown on the first row
            // test that the first feature is selected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: false
            };
            grid.fireEvent("rowmousedown", grid, 0, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[0]) > -1,
                 "click on row 0 selects feature 0");
                 
            // simulate a mousedown on the first row
            // test that the first feature is deselected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: true
            };
            grid.fireEvent("rowmousedown", grid, 0, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[0]) < 0,
                 "click on row 0 deselects feature 0");

            // simulate a mousedown on the second row
            grid.fireEvent("rowmousedown", grid, 1, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[1]) > -1,
                 "click on row 1 selects feature 1");

            sm.clearSelections();

            // select feature 0
            // test that the first row is selected
            sm.selectControl.select(features[0]);
            t.ok(sm.isSelected(0),
                 "selecting feature 0 selects row 0");

            // select feature 1
            // test that the second row is selected
            sm.selectControl.select(features[1]);
            t.ok(sm.isSelected(1),
                 "selecting feature 1 selects row 1");

            // unselect feature 0
            // test that the first row is selected
            sm.selectControl.unselect(features[0]);
            t.ok(!sm.isSelected(0),
                 "unselecting feature 0 unselects row 0");

            // unselect feature 1
            // test that the second row is unselected
            sm.selectControl.unselect(features[1]);
            t.ok(!sm.isSelected(1),
                 "unselecting feature 1 unselects row 1");
            
            /*
             * Tear down
             */
            grid.destroy();
        }
        
        
        function test_bind_unbind(t) {
            t.plan(9);

            /*
             * Set up
             */

            var map, layer, selectControl, features, store, sm, grid, e;

            map = new OpenLayers.Map('map');

            layer = new OpenLayers.Layer.Vector("vector");
            map.addLayer(layer);

            selectControl = new OpenLayers.Control.SelectFeature(layer);
            map.addControl(selectControl);

            features = [
                new OpenLayers.Feature.Vector(null,
                    {foo: "foo1", bar: "bar1"}
                ),
                new OpenLayers.Feature.Vector(null,
                    {foo: "foo2", bar: "bar2"}
                )
            ];
            
            store = new GeoExt.data.FeatureStore({
                layer: layer,
                data: features
            });

            sm = new GeoExt.grid.FeatureSelectionModel({
                layerFromStore: false
            });

            grid = new Ext.grid.GridPanel({
                renderTo: "grid",
                store: store,
                columns: [{
                    dataIndex: "foo"
                }, {
                    dataIndex: "bar"
                }],
                sm: sm,
                deferRowRender: false
            });
 
            /*
             * Test
             */

            // simulate a mousedown on the first row
            // test that the first feature is not selected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: false
            };
            grid.fireEvent("rowmousedown", grid, 0, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[0]) < 0,
                 "click on row 0 does not select feature 0");
            sm.clearSelections();
            
            // select feature 0
            // test that the first row is not selected
            selectControl.select(features[0]);
            t.ok(!sm.isSelected(0),
                 "selecting feature 0 does not select row 0");
            selectControl.unselect(features[0]);

            // bind the select control to the selection model
            sm.bind(selectControl);

            // simulate a mousedown on the second row
            // test that the second feature is selected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: false
            };
            grid.fireEvent("rowmousedown", grid, 1, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[1]) > -1,
                 "click on row 1 selects feature 1");
            sm.clearSelections();
            
            // select feature 1
            // test that the second row is selected
            selectControl.select(features[1]);
            t.ok(sm.isSelected(1),
                 "selecting feature 1 selects row 1");
            selectControl.unselect(features[1]);
            sm.clearSelections();
            
            // unbind row and feature selection
            sm.unbind(); 
            // (side effect: selectControl is deactivated)
            
            // simulate a mousedown on the first row
            // test that the first feature is not selected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: false
            };
            grid.fireEvent("rowmousedown", grid, 0, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[0]) < 0,
                 "click on row 0 does not select feature 0");
            sm.clearSelections();
            
            // select feature 0
            // test that the first row is not selected
            selectControl.select(features[0]);
            t.ok(!sm.isSelected(0),
                 "selecting feature 0 does not select row 0");
            selectControl.unselect(features[0]);
            
            // bind selection of features on a layer to rows
            sm.bind(layer, {controlConfig: {hover: true}});
            
            // verify that controlConfig has been applied
            t.eq(sm.selectControl.hover, true,
                 "bind configures correctly the select feature control");

            // simulate a mousedown on the second row
            // test that the second feature is selected in the layer
            e = {
                button: 0,
                shiftKey: false,
                ctrlKey: false
            };
            grid.fireEvent("rowmousedown", grid, 1, e);
            t.ok(OpenLayers.Util.indexOf(layer.selectedFeatures,
                                         features[1]) > -1,
                 "click on row 1 selects feature 1");
            sm.clearSelections();
            
            // select feature 1
            // test that the second row is selected
            sm.selectControl.select(features[1]);
            t.ok(sm.isSelected(1),
                 "selecting feature 1 selects row 1");
            sm.selectControl.unselect(features[1]);
            sm.clearSelections();
            
            /*
             * Tear down
             */
            grid.destroy();
        }
    </script>

  <body>
      <div id="map" style="width:100px;height:100px"></div>
      <div id="grid"></div>
  </body>
</html>
